﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.Windows.Threading;

namespace UI_Layer.Pages
{
    /// <summary>
    /// Interaction logic for TimeLeft.xaml
    /// </summary>
    public partial class TimeLeft : Page
    {
        public TimeLeft()
        {
            InitializeComponent();
            DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
            {
                this.BookingInfo_Clock.Content = DateTime.Now.ToString("HH:mm:ss");
            }, this.Dispatcher); 
            
        }

        private void ButtonClick_GoBack(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();

        }

        private void ButtonClick_GETBOOKINGINFO(object sender, RoutedEventArgs e)
        {
           
            Border_Rectangle.Visibility = Visibility.Hidden;
            Booking_Number_Label.Visibility = Visibility.Hidden;
            Booking_Number_Label_Input.Visibility = Visibility.Hidden;

            Plate_Number_Label.Visibility = Visibility.Hidden;
            Plate_Number_Label_Input.Visibility = Visibility.Hidden;          

            Status_Label.Visibility = Visibility.Hidden;
            Status_Label_Input.Visibility = Visibility.Hidden;
           
            Booked_Time_Label.Visibility = Visibility.Hidden;
            Booked_Time_Label_Input.Visibility = Visibility.Hidden;
            
            Time_Remaining_Label.Visibility = Visibility.Hidden;
            Time_Remaining_Label_Input.Visibility = Visibility.Hidden;
            
            Parser parser = new Parser();
            String[] booking = new String[5];

            try
            {
                int booking_number = Convert.ToInt32(Booking_Number_TextBOX.Text);
                booking = parser.getBooking(booking_number);

                string booking_nr = booking[0];

                if (booking_nr == "0")
                {
                    MessageBox.Show("Booking Number does not exist.");
                }
                else
                {
                    Border_Rectangle.Visibility = Visibility.Visible;
                    Booking_Number_Label.Visibility = Visibility.Visible;
                    Booking_Number_Label_Input.Visibility = Visibility.Visible;
                    Booking_Number_Label_Input.Content = booking[0];

                    Plate_Number_Label.Visibility = Visibility.Visible;
                    Plate_Number_Label_Input.Visibility = Visibility.Visible;
                    Plate_Number_Label_Input.Content = booking[1];

                    Status_Label.Visibility = Visibility.Visible;
                    Status_Label_Input.Visibility = Visibility.Visible;
                    Status_Label_Input.Content = booking[3];

                    Booked_Time_Label.Visibility = Visibility.Visible;
                    Booked_Time_Label_Input.Visibility = Visibility.Visible;
                    Booked_Time_Label_Input.Content = booking[4] + " hour(s)";

                    Time_Remaining_Label.Visibility = Visibility.Visible;
                    Time_Remaining_Label_Input.Visibility = Visibility.Visible;
                    string timeRemaining = calculateTimeRemaining(booking[2], booking[4]);

                    Time_Remaining_Label_Input.Content = timeRemaining;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }          
            
        }

        private string calculateTimeRemaining(string bookedate, string bookedTime)
        {
            DateTime dateNow = DateTime.Now;
            DateTime dateBooked = Convert.ToDateTime(bookedate);

            double elapsedTimetempHours = (dateNow - dateBooked).TotalHours;
            double elapsedTimetempMinutes = (dateNow - dateBooked).TotalMinutes;

            double elapsedTimeHours = Math.Round(elapsedTimetempHours, 1);
            double elapsedTimeMinutes = Math.Round(elapsedTimetempMinutes, 1);

            double remainingTimetempHours = Convert.ToDouble(bookedTime) - elapsedTimeHours;
            double remainingTimeHours = Math.Round(remainingTimetempHours, 2);

            double remainingTimetempMinutes = Convert.ToDouble(bookedTime) * 60 - elapsedTimeMinutes;
            double remainingTimeMinutes = Math.Round(remainingTimetempMinutes, 1); 

            if (remainingTimeMinutes < 0)
            {
                return "Time expired";
            }
            else
            {
                return Convert.ToString(remainingTimeMinutes) + " min " + " (" + remainingTimeHours +" hours)";
            }
        }

        private void Booking_Number_TextBOX_IsKeyboardFocusedChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            GetBooking_Button.Visibility = Visibility.Visible;

        }

        private void Booking_Number_TextBOX_LostFocus(object sender, RoutedEventArgs e)
        {
            GetBooking_Button.Visibility = Visibility.Hidden;
        }
        
    }
}
